import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///特殊作业-多人员签字
///[title] 标题
///[onCallBack] 输入回调
///[isRequired] 是否必填
///[isEnabled] 是否可编辑
///[isChange] 是否在交接班中
class CustomMultiSignaturePage extends StatefulWidget {
  const CustomMultiSignaturePage({
    required this.title,
    required this.onCallBack,
    super.key,
    this.signatureLists,
    this.isRequired,
    this.isChange = false,
    this.isEnabled = true,
  });

  final String title;
  final Function(List<String> signatureLists) onCallBack;
  final List<String>? signatureLists;
  final bool? isRequired;
  final bool isEnabled;
  final bool isChange;

  @override
  State<CustomMultiSignaturePage> createState() =>
      _CustomMultiSignaturePageState();
}

class _CustomMultiSignaturePageState extends State<CustomMultiSignaturePage> {
  List<String> signatureLists = [];

  @override
  void initState() {
    super.initState();
    if (widget.signatureLists != null) {
      signatureLists.addAll(widget.signatureLists!);
    }
  }

  ///回调给外部
  void _callBack() {
    widget.onCallBack(signatureLists);
  }

  ///获取签名选择样式
  Widget _getSignatureSelectView() {
    return Row(
      children: [
        Text(
          '*',
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            color: AppColors.meaning_color_danger,
          ),
        ),
        Expanded(
            child: Text(
          widget.title,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.1,
            color: AppColors.font_color_black,
          ),
        )),
        Text(
          widget.isEnabled ? '添加签名' : '',
          textAlign: TextAlign.end,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            color: AppColors.text_grey,
          ),
        ),
        Visibility(
            visible: widget.isEnabled,
            child: Row(
              children: [
                AppGaps.hGap6,
                Icon(Icons.arrow_forward_ios,
                    size: 14.sp, color: AppColors.text_grey)
              ],
            )),
      ],
    ).clickOnce(() {
      if (!widget.isEnabled) {
        return;
      }
      FocusScope.of(context).unfocus();
      Get.toNamed(AppRoutes.signature)?.then((value) {
        //如果不为空则设置签名
        if (value != null) {
          signatureLists.add(value.toString());
          _callBack();
          setState(() {});
        }
      });
    });
  }

  Widget _getSignatureWarpView() {
    return Wrap(
      // spacing: 2.w, // 主轴(水平)方向间距
      runSpacing: 10.w, // 纵轴（垂直）方向间距
      // alignment: WrapAlignment.start, //沿主轴方向居中
      direction: Axis.horizontal,
      children: signatureLists.map<Widget>((item) {
        return Stack(
          alignment: Alignment.topRight,
          children: [
            //获取作业人信息
            Container(
              margin: EdgeInsets.only(top: 8.w, right: 8.w),
              width: 76.w,
              height: 36.w,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4.w),
                  color: Colors.white,
                  border: Border.all(
                    color: const Color(0xFFEFF2F6),
                    width: 1,
                  ),
                  image: DecorationImage(
                    image: NetworkImage(Interface.fileUrl + item),
                  )),
            ).clickShowLarge(url: Interface.fileUrl + item),
            //删除按钮
            Visibility(
                visible: widget.isEnabled && !widget.isChange,
                child: Icon(
                  Icons.highlight_remove,
                  size: 18.w,
                  color: const Color(0xFFFD6B6B),
                ).clickOnce(() {
                  signatureLists.remove(item);
                  _callBack();
                  setState(() {});
                }))
          ],
        );
      }).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      AppGaps.vGap20,
      _getSignatureSelectView(),
      Visibility(
        visible: signatureLists.isNotEmpty,
        child: AppGaps.vGap12,
      ),
      _getSignatureWarpView(),
      AppGaps.vGap20,
    ]);
  }
}
